package com.bxd.baodan.adapter;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.BaseActivity;
import com.bxd.baodan.activity.WebViewPager;
import com.bxd.baodan.item.InsuranceItemBean;
import com.bxd.baodan.util.GlideUtil;
import com.bxd.baodan.util.StringUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

public class BaoDanAdapter extends BaseAdapter {

    private final int JI_JIANG_SHENG_XIAO = 1;
    private final int BAO_ZHANG_ZHONG = 2;
    private final int JI_JIANG_DAO_QI = 3;
    private final int YI_SHI_XIAO = 4;

    private final int TU_PIAN = 2;
    private final int SHOU_DONG = 1;

    private Context mContext;
    private List<InsuranceItemBean.ListBean> mData;

    public BaoDanAdapter(Context context) {
        mContext = context;
    }

    public BaoDanAdapter(Context context, List<InsuranceItemBean.ListBean> data) {
        mContext = context;
        mData = data;
    }

    public void setData(List<InsuranceItemBean.ListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mData != null && mData.size() > 0) {
            if (mDataIsNullListener != null) {
                mDataIsNullListener.DataIsNull(false);
            }
            return mData.size();
        }
        if (mDataIsNullListener != null) {
            mDataIsNullListener.DataIsNull(true);
        }
        return 0;
    }

    @Override
    public InsuranceItemBean.ListBean getItem(int position) {
        if (mData != null && mData.size() > 0) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_insurance, null);
            new ViewHolder(convertView);
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        final InsuranceItemBean.ListBean bean = mData.get(position);

        if (TextUtils.isEmpty(bean.company.name)) {
            holder.tvInsuranceName.setText(bean.category.name);

        } else {
            holder.tvInsuranceName.setText(bean.category.name + "·" + bean.company.name);
        }
        showType(holder, bean.type);
        String[] images = bean.image;
        if (images != null && images.length > 0) {
//            holder.ivImage.setImageResource();
            GlideUtil.loadImage(mContext, images[0], holder.ivImage);
        }
        holder.tvUserName.setText(bean.insured);
        holder.tvProductName.setText(bean.productName);
        holder.tvTime.setText(bean.expireStart + " 至 " + bean.expireEnd);
        showLabel(holder, bean.status);
        if (bean.evaluationProgress ) {
            holder.can_click.setVisibility(View.VISIBLE);
            holder.no_click.setVisibility(View.GONE);
        } else {
            holder.can_click.setVisibility(View.GONE);
            holder.no_click.setVisibility(View.VISIBLE);
        }

        holder.can_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 18/1/15 新添的url
                if (!StringUtil.isEmpty(bean.url)) {
                    MobclickAgent.onEvent(App.getContext(), "v6_1_3_SY_bdgl_ckcpbg");
                    ((BaseActivity)mContext).openActivity(WebViewPager.class,new String[]{"startUrl"},new String[]{bean.url});
                }
            }
        });


        return convertView;
    }

    public void showLabel(ViewHolder viewHolder, String label) {
        int index = 0;
        try {
            index = Integer.parseInt(label);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return;
        }

        viewHolder.tvSecurity.setVisibility(View.GONE);
        viewHolder.tvExpiring.setVisibility(View.GONE);
        viewHolder.tvWillTakeEffect.setVisibility(View.GONE);
        viewHolder.tvFailure.setVisibility(View.GONE);
        switch (index) {
            case JI_JIANG_SHENG_XIAO:
                viewHolder.tvWillTakeEffect.setVisibility(View.VISIBLE);
                break;
            case BAO_ZHANG_ZHONG:
                viewHolder.tvSecurity.setVisibility(View.VISIBLE);
                break;
            case JI_JIANG_DAO_QI:
                viewHolder.tvExpiring.setVisibility(View.VISIBLE);
                break;
            case YI_SHI_XIAO:
                viewHolder.tvFailure.setVisibility(View.VISIBLE);
                break;
        }
    }

    public void showType(ViewHolder viewHolder, String type) {
        int index = 0;
        try {
            index = Integer.parseInt(type);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return;
        }

        if (index == TU_PIAN) {
            viewHolder.ivImage.setVisibility(View.VISIBLE);
            viewHolder.llManual.setVisibility(View.GONE);
        }
        if (index == SHOU_DONG) {
            viewHolder.ivImage.setVisibility(View.GONE);
            viewHolder.llManual.setVisibility(View.VISIBLE);
        }
    }

    class ViewHolder {
        public TextView tvSecurity;
        public TextView tvExpiring;
        public TextView tvWillTakeEffect;
        public TextView tvFailure;
        public TextView tvInsuranceName;
        public LinearLayout llManual;
        public ImageView ivImage;

        public TextView tvUserName;
        public TextView tvProductName;
        public TextView tvTime;
        public View can_click, no_click;


        ViewHolder(View view) {
            tvInsuranceName = (TextView) view.findViewById(R.id.tv_insurance_name);
            tvSecurity = (TextView) view.findViewById(R.id.tv_security); // 保障中
            tvExpiring = (TextView) view.findViewById(R.id.tv_expiring);// 到期
            tvWillTakeEffect = (TextView) view.findViewById(R.id.tv_will_take_effect); // 生效
            tvFailure = (TextView) view.findViewById(R.id.tv_failure); // 失效
            llManual = (LinearLayout) view.findViewById(R.id.ll_manual);
            ivImage = (ImageView) view.findViewById(R.id.iv_image);

            tvUserName = (TextView) view.findViewById(R.id.tv_user_name);
            tvProductName = (TextView) view.findViewById(R.id.tv_product_name);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            can_click = view.findViewById(R.id.can_click);
            no_click = view.findViewById(R.id.no_click);

            view.setTag(this);
        }
    }

    private DataIsNullListener mDataIsNullListener;

    public void setDataIsNullListener(DataIsNullListener mDataIsNullListener) {
        this.mDataIsNullListener = mDataIsNullListener;
    }

    public interface DataIsNullListener {
        void DataIsNull(boolean b);
    }


}
